/*
 * Reminder.java
 *
 * Created on 1 de mayo de 2006, 08:43 PM
 */

package taladro.control;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;

import controles.interfaz.Consola;
import taladro.puerto.PuertoParalelo;

/**
 *
 * @author Administrator
 */
public class Reminder {
    Timer timer;
    private PuertoParalelo puerto;
    private Secuencia sec;

    public Reminder(int miliseconds) {
        puerto = PuertoParalelo.getInstance();
        puerto.setPuerto("LPT1",0);
        //sec = new taladro.control.SecuenciaSimple();
        sec = new taladro.control.SecuenciaMedioPaso();
        sec.setDirection( Secuencia.AVANZAR );
        
        timer = new Timer();
        //timer.schedule(new RemindTask(),0, miliseconds);

        Producer p1 = new Producer(miliseconds);
        
        p1.start();
        try{
        p1.join();
        }
        catch(Exception e){
           e.printStackTrace();
        }
        IConsole consolas[]=ConsolePlugin.getDefault().getConsoleManager().getConsoles();
        ((Consola)consolas[0]).imprime("Finalizado");
        System.out.println("Finalizado");
        Date date = new Date();
        ((Consola)consolas[0]).imprime( date.toString() + " " + date.getTime() );
        System.out.println( date.toString() + " " + date.getTime() );
	}

    class RemindTask extends TimerTask {
        int times = 120;
        public void run() {
            if( times-- < 0  ){
                timer.cancel(); //Terminate the timer thread
                Date date = new Date();
                System.out.println( date.toString() + " " + date.getTime() );
            }
                
            //System.out.print("Time's up!\n");
            Date date = new Date();
            System.out.println( "" + date.getTime() );
            puerto.outPort((byte) (0x60 | sec.getNext() ) );
            //date = new Date();
            //System.out.println( "" + date.getTime() );
        }
    }
    
    class Producer extends Thread {
    private long number;
    private SystemTimer timer;

    public Producer(long number) {
        this.number = number;
        timer = new SystemTimer();
    }

    public void run() {
        System.out.println( "***Inicia slep java" + timer.getTime() );
        for (int i = 0; i < 100; i++) {
            //cubbyhole.put(number, i);
            //System.out.println("time up!");
            //Date date = new Date();
            //System.out.println( "" + System.currentTimeMillis() );
            System.out.println( "" + timer.getTime() );
            //timer.sleep(number);            
            try {
            //timer.sleep(number+2);
            sleep(number);
            puerto.outPort((byte) (0x60 | sec.getNext() ) );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        System.out.println("Cambio**********************************************");
        System.out.println( "***Inicia slep advanced timer" + timer.getTime() );
        for (int i = 0; i < 100; i++) {
            //cubbyhole.put(number, i);
            //System.out.println("time up!");
            Date date = new Date();
            System.out.println( "" + timer.getTime() );
            //try {
            timer.sleep(number);
            //timer.sleep(number-4);
            //sleep(1);
            puerto.outPort((byte) (0x60 | sec.getNext() ) );
            //} catch (InterruptedException e) {
                //e.printStackTrace();
            //}
        }
        System.out.println( "***Termina slep advanced timer" + timer.getTime() );

    }
}

    public static void main(String args[]) {
        Date date = new Date();
        System.out.println( date.toString() +  " " + date.getTime() );
        new Reminder(2);
        //System.nanoTime();
    }
}